package vcp.gui.frames;

import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;

import control.SimpleClient;
import vcp.gui.components.VCPFrame;
import vcp.gui.components.VCPMenu;
import vcp.gui.forms.CancelOrderForm;
import vcp.gui.forms.ComplaintForm;

import vcp.gui.forms.SaveParkingForm;
import vcp.gui.forms.TrackOrderForm;
import vcp.gui.guest.screens.ScreenFormWrapper;
import vcp.gui.guest.screens.ScreenMain;
import vcp.gui.guest.screens.ScreenOrders;
import vcp.gui.utils.MenuBuilder;

public class GuestFrame extends VCPFrame {
	/* leftPane menu */
	private VCPMenu menu;
	/* menu items */
	private JButton MenuItmNewOrder;
	private JButton MenuItmTrackOrder;
	private JButton MenuItmCanellOrder;
	private JButton MenuItmSaveParking;
	private JButton MenuItmComplaint;
	
    
	private ScreenMain mainScreen;
	private ScreenOrders screenOrders;
	private CancelOrderForm cancelOrderForm;
	private ComplaintForm complaintForm;
	private TrackOrderForm trackOrderForm;
	private SaveParkingForm saveParkingFormForm;
	
	@SuppressWarnings("unused")
	private SimpleClient client;

	public GuestFrame() {
		super();
		initialize("localhost",12345);
		 client.setPort(12345);
		 client.setHost("localhost");
		 open();
	}
   //
	private void initialize(String host, int port) {
		
		this.client = new SimpleClient(host,port);

		this.setTitle("VCP Customers");
		this.mainScreen = new ScreenMain(getMenu());
		this.screenOrders = new ScreenOrders(client);
		this.cancelOrderForm = new CancelOrderForm(client);
		this.complaintForm = new ComplaintForm(client);
		this.trackOrderForm = new TrackOrderForm();
		this.saveParkingFormForm = new SaveParkingForm();
		this.changePanel(mainScreen);
		
		
	}

	private VCPMenu getMenu() {
		MenuBuilder builder = new MenuBuilder(new Insets(5,5,5,5), 
				                              new GridLayout(4,2,5,5));
		builder.addItem(getItmNewOrder("New Order"));
		builder.addItem(getMenuItmSaveParking("Save Parking"));
		
		builder.addItem(getMenuItmCanellOrder("Cancell Order"));
		builder.addItem(getMenuItmComplaint("Complaints"));
		builder.addItem(getMenuItmTrackOrder("Track Order"));		
		return (VCPMenu) builder.getMenu();
	}
	
	public void goToMain(){
		this.changePanel(mainScreen);
	}
	
	
/* MENU ITEMS */
	
	private JButton getItmNewOrder(String text) {
		if (MenuItmNewOrder == null) {
			MenuItmNewOrder = new JButton(text);
			MenuItmNewOrder.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
                      changePanel(new ScreenFormWrapper(screenOrders));                   
				}
			});
		}
		return MenuItmNewOrder;
	}
	
	private JButton getMenuItmCanellOrder(String text) {
		if (MenuItmCanellOrder == null) {
			MenuItmCanellOrder = new JButton(text);
			MenuItmCanellOrder.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					changePanel(new ScreenFormWrapper(cancelOrderForm));    
				}
			});
		}
		return MenuItmCanellOrder;
	}
	
	private JButton getMenuItmComplaint(String text) {
		if (MenuItmComplaint == null) {
			MenuItmComplaint = new JButton(text);
			MenuItmComplaint.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
                       changePanel(new ScreenFormWrapper(complaintForm));
				}
			});
		}
		return MenuItmComplaint;
	}
	
	private JButton getMenuItmTrackOrder(String text) {
		if (MenuItmTrackOrder == null) {
			MenuItmTrackOrder = new JButton(text);
			MenuItmTrackOrder.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					  changePanel(new ScreenFormWrapper(trackOrderForm));    
				}
			});
		}
		return MenuItmTrackOrder;
	}
	
	private JButton getMenuItmSaveParking(String text) {
		if (MenuItmSaveParking == null) {
			MenuItmSaveParking = new JButton(text);
			MenuItmSaveParking.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					changePanel(new ScreenFormWrapper(saveParkingFormForm));  
				}
			});
		}
		return MenuItmSaveParking;
	}

public void close()
	  {
	    try {
	    	//TODO: add set host and port?
	    	client.closeConnection();
	    }
	    catch (Exception ex)
	    {
		     System.out.println(ex.toString());

	    }
	  }

	  public void open()
	  {
	    try {
	      client.openConnection();
	    }
	    catch (Exception ex)
	    {   
		      System.out.println(ex.toString());
	    }
	  }

}
